$(function () {
    var afs_token;
    pointman.use('do', function () {
        var afs = pointman.getConfig();
        var afs_appkey = afs.appkey;
        var afs_scene = afs.scene;
        afs_token = afs.token;
    });
    var RegisterMobile;//手机号
    h5ajax({
        url: '/api/Product/GetTransactionData',
        method: 'POST',
        data: {

        }
    }).then(function (res) {
        if (res.code == 1) {
            $('#registerNumber').text(res.data.registerNumber + '人');
            $('#totalMoney').text(res.data.totalMoney.toFixed(0) + '万元');
            $('#day').text(res.data.day + '天');
        } else {
            layer.closeAll();

        }
    }).fail(function (err) {
        isRegister = true;
        layer.closeAll();
        layer.open({ content: '请求失败', skin: 'msg', time: 2 });
    });
    var mobileReg = /1[3-9]{1}[0-9]{9}/;//正则验证input
    var verifyCodeReg = /\d{5}/;
    var passwordReg = /\S{6,20}/;
    var verifyInterval
    $('#indexbtn').click(function () {//首页弹出框
        h5ajax({
            url: '/api/bank/gettime',
            method: 'POST',
            data: {

            }
        }).then(function (res) {
            if (res.code == 1) {
                var severseTime = new Date(new Date(res.data.replace(/-/g, '/')).getTime());
                var month = severseTime.getMonth() + 1;
                if (month == 4) {
                    clearInterval(verifyInterval);
                    if ($('.pop').length) {
                        layer.open({
                            type: 1,
                            anim: 2,
                            shadeClose: true, //开启遮罩关闭
                            content: $('.pop').html()
                        });
                    }
                    $('.closebox_img').click(function () {
                        layer.closeAll();
                    });
                    var clickTime = function () {//倒计时函数
                        verifyNum = 60;
                        isGetVer = false;
                        $('.layui-m-layercont #vercodebtn').text("60s");
                        verifyInterval = setInterval(function () {
                            verifyNum--;
                            if (verifyNum <= 0) {
                                clearInterval(verifyInterval);
                                isGetVer = true;
                                isSendVerify = true;
                                $('.layui-m-layercont #vercodebtn').text('重新获取');
                                $('.layui-m-layercont #vercodebtn').click(function () {
                                    if (verifyNum == 0) {
                                        clickTime();
                                    }
                                })
                            } else {
                                $('.layui-m-layercont #vercodebtn').text(verifyNum + "s");
                            }
                        }, 1000);
                        h5ajax({//获取验证码
                            url: '/api/User/GetMobileVerifyCode',
                            method: 'POST',
                            data: {
                                mobile: RegisterMobile,
                                IsExistUser: 0,
                                tokenId: afs_token
                            }
                        }).then(function (res) {
                            if (res.code == 1) {
                            } else {
                                isRegister = true;
                                layer.closeAll();
                                if (res.code == 1083) {//同盾弹框
                                    var html = '';
                                    html += '<div class="td_bg">';
                                    html += '     <span class="td_close">';
                                    html += '    </span>';
                                    html += '</div>';
                                    layer.open({
                                        type: 1,
                                        shade: false,
                                        title: false,
                                        content: html
                                    });
                                    var Tdparent = document.querySelector('.td_bg');
                                    var Tdclose = document.querySelector('.td_close');
                                    Tdparent.parentNode.parentNode.style.background = "rgba(255,255,255,0)";
                                    Tdparent.parentNode.parentNode.style.boxShadow = 'none'
                                    Tdclose.onclick = function () {
                                        layer.closeAll();
                                    }
                                } else {
                                    layer.open({
                                        content: res.data, skin: 'msg', time: 2 //2秒后自动关闭
                                    });
                                }
                            }
                        }).fail(function (err) {
                            isRegister = true;
                            layer.closeAll();
                            layer.open({ content: '请求失败', skin: 'msg', time: 2 });
                        });
                    }
                    $('.layui-m-layercont #nextBtn').click(function () {//老用户下一步
                        RegisterMobile = $('.layui-m-layercont #mobile').val()
                        if (!mobileReg.test(RegisterMobile)) {
                            layer.open({
                                content: '您输入的手机号有误，请重新输入', skin: 'msg', time: 2
                            });
                            return false;
                        }
                        var index = layer.open({ type: 2 });
                        h5ajax({
                            url: '/api/Promotion/SentPluisForWeChat',
                            method: 'POST',
                            data: {
                                mobile: RegisterMobile
                            }
                        }).then(function (res) {
                            layer.close(index);
                            if (res.code == 1) {
                                var count = 0;
                                function getResult() {
                                    h5ajax({
                                        url: '/api/Promotion/SentPluisForWeChatState',
                                        method: 'POST',
                                        data: {
                                            key: res.data
                                        }
                                    }).then(function (result) {
                                        if (result.code == 1) {
                                            layer.close(index)
                                            if (result.data) {
                                                result.data = JSON.parse(result.data);
                                            }
                                            if (result.data.Result == 'unregister') {
                                                $('.layui-m-layercont #pop_up_old').hide();
                                                $('.layui-m-layercont #pop_up_reg').show();
                                                clickTime();
                                            } else if (result.data.Result == 'repeat') {
                                                window.location.href = "/spread/oneyear/repeat.html";
                                            } else if (result.data.Result == 'success') {
                                                window.location.href = "/spread/oneyear/success.html";
                                            } else {
                                                layer.open({
                                                    content: '网络故障，请重新领取加息券', skin: 'msg', time: 2
                                                });
                                            }
                                        } else if (result.code == 3 && result.data == 'retry') {
                                            if (count < 500) {
                                                count++;
                                                setTimeout(function () {
                                                    getResult();
                                                }, 500)
                                            }
                                        } else {
                                            layer.close(index)
                                        }
                                    }).fail(function (err) {
                                        layer.close(index)
                                        layer.open({ content: '请求失败', skin: 'msg', time: 2 });
                                    });
                                }
                                getResult();
                            } else {
                                layer.close(index)
                            }
                        }).fail(function (err) {
                            layer.close(index)
                            layer.open({ content: '请求失败', skin: 'msg', time: 2 });
                        });
                    })
                    $('.layui-m-layercont #newNext').click(function () {//新用户下一步

                        var verifyCode = $('.layui-m-layercont #vercode').val();//验证码
                        var password = $('.layui-m-layercont #password').val();
                        if (!verifyCodeReg.test(verifyCode)) {
                            layer.open({
                                content: '您输入的验证码有误，请重新输入', skin: 'msg', time: 2
                            });
                            return false;
                        }
                        if (!passwordReg.test(password)) {
                            layer.open({
                                content: '请设置6-20位登录密码', skin: 'msg', time: 2
                            });
                            return false;
                        }
                        var index = layer.open({ type: 2 });
                        h5ajax({//先注册
                            url: '/api/user/Register',
                            method: 'POST',
                            data: {
                                userfromid: 0,
                                username: RegisterMobile,
                                password: password,
                                verifycode: verifyCode,
                                clientId: '',
                                geTuiAppId: '',
                                invitationcode: '',//分享码
                                tokenId: afs_token
                            }
                        }).then(function (res) {
                            if (res.code == 1) {//注册成功
                                h5ajax({//执行查询
                                    url: '/api/Promotion/SentPluisForWeChat',
                                    method: 'POST',
                                    data: {
                                        mobile: RegisterMobile
                                    }
                                }).then(function (res) {
                                    if (res.code == 1) {
                                        var count = 0;
                                        function getResult() {
                                            h5ajax({
                                                url: '/api/Promotion/SentPluisForWeChatState',
                                                method: 'POST',
                                                data: {
                                                    key: res.data
                                                }
                                            }).then(function (result) {
                                                if (result.code == 1) {
                                                    layer.close(index)
                                                    if (result.data) {
                                                        result.data = JSON.parse(result.data);
                                                    }
                                                    if (result.data.Result == 'repeat') {
                                                        window.location.href = "/spread/oneyear/repeat.html";
                                                    } else if (result.data.Result == 'success') {
                                                        window.location.href = "/spread/oneyear/newsuccess.html";
                                                    } else {
                                                        layer.open({
                                                            content: '网络故障，请重新领取加息券', skin: 'msg', time: 2
                                                        });
                                                    }
                                                } else if (result.code == 3 && result.data == 'retry') {
                                                    if (count < 500) {
                                                        count++;
                                                        setTimeout(function () {
                                                            getResult();
                                                        }, 500)
                                                    }
                                                } else {
                                                    layer.close(index)
                                                }
                                            }).fail(function (err) {
                                                layer.close(index)
                                                layer.open({ content: '请求失败', skin: 'msg', time: 2 });
                                            });
                                        }
                                        getResult();
                                    } else {
                                        layer.close(index)
                                    }
                                }).fail(function (err) {
                                    layer.close(index)
                                    layer.open({ content: '请求失败', skin: 'msg', time: 2 });
                                });
                            } else {
                                isRegister = true;
                                layer.close(index)
                                if (res.code == 1083) {//同盾弹框
                                    layer.closeAll();
                                    var html = '';
                                    html += '<div class="td_bg">';
                                    html += '     <span class="td_close">';
                                    html += '    </span>';
                                    html += '</div>';
                                    layer.open({
                                        type: 1,
                                        shade: false,
                                        title: false,
                                        content: html
                                    });
                                    var Tdparent = document.querySelector('.td_bg');
                                    var Tdclose = document.querySelector('.td_close');
                                    Tdparent.parentNode.parentNode.style.background = "rgba(255,255,255,0)";
                                    Tdparent.parentNode.parentNode.style.boxShadow = 'none'
                                    Tdclose.onclick = function () {
                                        layer.closeAll();
                                    }
                                } else {
                                    layer.open({
                                        content: res.data, skin: 'msg', time: 2 //2秒后自动关闭
                                    });
                                }
                            }
                        }).fail(function (err) {
                            layer.close(index)
                            layer.open({ content: '请求失败', skin: 'msg', time: 2 });
                        });
                    })
                } else if (month > 4) {
                    layer.open({ content: '活动已经结束，感谢您的关注', skin: 'msg', time: 2 });
                }
            } else {
            }
        }).fail(function (err) {
            isRegister = true;
            layer.closeAll();
            layer.open({ content: '请求失败', skin: 'msg', time: 2 });
        });
    });
})
